package com.itheima.web.servlet.system;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.SysLog;
import com.itheima.utils.BeanUtil;
import com.itheima.web.servlet.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.UUID;

/**
 * @Author:zhiGang.
 * @Description:
 * @Date:Created in 9:02 2020/11/30
 * @ModifiedBy:
 */
@WebServlet("/system/syslog")
public class SysLogServlet extends BaseServlet {
    /**
     * @Description: 分页
     * @Param: [req, resp]
     * @return: void
     * @Author: zhiGang.
     * @Date: 2020/11/30
     */
    public void findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 5;
        String visitTime = "";
        try {
            pageNum = Integer.valueOf(req.getParameter("pageNum"));
            pageSize = Integer.valueOf(req.getParameter("pageSize"));
            visitTime = req.getParameter("visitTime");
        } catch (Exception e) {
            e.printStackTrace();
        }
        PageHelper.startPage(pageNum, pageSize);

        List<SysLog> sysLogList = sysLogService.findAll(visitTime);

        PageInfo<SysLog> page = new PageInfo<>(sysLogList);
        // 将数据保存到指定位置
        req.setAttribute("page", page);
        // 跳转页面
        req.getRequestDispatcher("/WEB-INF/pages/system/syslog/syslog-list.jsp").forward(req, resp);
    }

    /**
     * @Description: 删除
     * @Param: [req, resp]
     * @return: void
     * @Author: zhiGang.
     * @Date: 2020/11/30
     */
    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String deleteArrays = "";
        try {
            deleteArrays = req.getParameter("deleteArrays");
        } catch (Exception e) {
            e.printStackTrace();
        }

        List<String> sysLogIds = new ObjectMapper().readValue(deleteArrays, new TypeReference<List<String>>() {
        });
        for (String sysLogId : sysLogIds) {
            sysLogService.delete(sysLogId);
        }
        resp.sendRedirect(req.getContextPath() + "/system/syslog?operation=findAll");
    }

    /**
         * @Description:  时间模糊搜索
         * @Param: [req, resp]
         * @return: void
         * @Author: zhiGang.
         * @Date:  2020/12/1
         */
    public void findByTime(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //将数据获取到,封装成一个对象
        String time = req.getParameter("time");

        SysLog sysLog = BeanUtil.fillBean(req, SysLog.class);
        //调用业务层接口
        sysLogService.findByTime(time);
        //跳转回到页面list
        resp.sendRedirect(req.getContextPath() + "/system/syslog?operation=findAll");
    }

   /**
     * @Description:  导出
     * @Param: [req, resp]
     * @return: void
     * @Author: zhiGang.
     * @Date:  2020/12/1
     */
    public void downloadReport(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //返回的数据类型为xlsx类型
        resp.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
        String fileName = new String("测试文件名.xlsx".getBytes(), "iso8859-1");
        resp.addHeader("Content-Disposition", "attachment;fileName=" + fileName);
        //生成报告的文件,然后传递到前端页面
        ByteArrayOutputStream os = sysLogService.getReport();
        //获取产生响应的流对象
        ServletOutputStream sos = resp.getOutputStream();
        //将数据从原始的字节流对象中提取出来写入到servlet对应的输出流中
        os.writeTo(sos);
        //将输出流刷新
        sos.flush();
        os.close();
    }
}
